var express = require("express");
var router = express.Router();
let crypto = require("crypto");
let jwt = require("jsonwebtoken");
let userServer = require("../server/userServer");

/* GET users listing. */
router.post("/token", function (req, res, next) {
  let { token } = req.body;
  jwt.verify(token, "abc", async (err, info) => {
    if (!err) {
      res.json({
        info,
      });
    } else {
      res.json({
        code: 0,
        msg: "token解析失败",
      });
    }
  });
});

//注册
router.post("/register", async (req, res, next) => {
  let { username, userpwd, usertype, usertime, useraccount, userphone } = req.body; //获取请求的参数值
  //查询用户名是否存在
  let result = await userServer.queryUserinfo({ username });
  if (result.length) {
    res.json({
      code: 0,
      msg: "该用户名已经存在",
    });
  } else {
    //需要对密码进行加密处理
    let md5 = crypto.createHash("md5");
    userpwd = md5.update(userpwd).digest("hex");
    let list = await userServer.addUserinfo([{ username, userpwd, usertype, usertime, useraccount, userphone }]);
    res.json({
      code: 1,
      msg: "注册成功",
      list,
    });
  }
});

//登录
router.post("/login", async (req, res, next) => {
  let { userpwd, useraccount } = req.body;
  let list = await userServer.queryUserinfo({ useraccount });
  if (list.length) {
    let md5 = crypto.createHash("md5");
    userpwd = md5.update(userpwd).digest("hex");
    let pwdlist = await userServer.queryUserinfo({ userpwd });
    if (pwdlist.length) {
      let info = list[0];
      let { username, _id, usertype, useraccount, userphone } = info; //取出用户信息
      let token = jwt.sign({ username, _id, usertype, useraccount, userphone }, "abc", { expiresIn: 60 * 60 }); //设置一个小时
      res.json({
        code: 1,
        msg: "登录成功",
        username,
        useraccount,
        userphone,
        token,
        usertype,
      });
    } else {
      res.json({
        code: 0,
        msg: "密码不匹配",
      });
    }
  } else {
    res.json({
      code: 0,
      msg: "用户名不存在",
    });
  }
});

//查询用户
router.get("/getuserinfo", async (req, res, next) => {
  let token = req.headers.token;
  let key = req.query.key; //获取查询的关键字
  jwt.verify(token, "abc", async (err, info) => {
    if (!err) {
      let usertype = info.usertype;
      if (usertype == 1) {
        //1表示超级管理员
        let params = {}; //参数
        if (key) {
          let reg = new RegExp(`${key}`);
          params.username = reg; //表示根据用户名来进行查找
        }
        let result = await userServer.queryUserinfo(params);
        res.json({
          code: 1,
          msg: info.username,
          result,
        });
      } else {
        res.json({
          code: 0,
          msg: "权限不够",
        });
      }
    } else {
      res.json({
        code: 0,
        msg: "token解析失败",
      });
    }
  });
});

//添加
router.post("/adduser", async (req, res, next) => {
  let token = req.headers.token;
  let { username, userpwd, usertype, useraccount, userphone } = req.body; //获取请求的参数值
  jwt.verify(token, "abc", async (err, info) => {
    if (!err) {
      let type = info.usertype;
      if (type == 1) {
        //查询用户名是否存在
        let result = await userServer.queryUserinfo({ useraccount });
        if (result.length) {
          res.json({
            code: 0,
            msg: "该账号已经存在",
          });
        } else {
          //需要对密码进行加密处理
          let md5 = crypto.createHash("md5");
          userpwd = md5.update(userpwd).digest("hex");
          let list = await userServer.addUserinfo([{ username, userpwd, usertype, usertime: 0, checked: false, useraccount, userphone }]);
          res.json({
            code: 1,
            msg: "注册成功",
            list,
          });
        }
      }
    } else {
      res.json({
        code: 0,
        msg: "token解析失败",
      });
    }
  });
});

module.exports = router;
